package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.StageDefinePropertyListAdapter;
import com.isunland.managesystem.base.BaseEntity;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.DataStatus;
import com.isunland.managesystem.entity.ProjectLedger;
import com.isunland.managesystem.entity.StageDefinePropertyOriginal;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 对象功能:  项目台账登记自定义属性
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/5/15 14:11
 */
public class ProjectLedgerPropertyListFrament extends BaseListFragment {

  private CurrentUser mUser;

  private ProjectLedger projectContent;
  private int DETAIL = 0;
  private ArrayList<StageDefinePropertyOriginal.StageDefinePropertyContent> mList;
  private StageDefinePropertyListAdapter madapter;

  @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    projectContent =
        (ProjectLedger) getArguments().getSerializable(ProjectLedgerPagerActivity.EXTRA_CONTENT);
    setIsPaging(true);
    mUser = CurrentUser.newInstance(getActivity());
    mList = new ArrayList<>();
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_add, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.menu_item_add:
        if (isAllowOprate()) {
          Intent i = new Intent(getActivity(), ProjectLedgerPropertyDetailActivity.class);
          i.putExtra(ProjectLedgerPropertyDetailFragment.EXTRA_VALUE, projectContent);
          startActivityForResult(i, DETAIL);
        }
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  /* 允许操作条件 */
  public boolean isAllowOprate() {
    if ((DataStatus.NEW.equalsIgnoreCase(projectContent.getDataStatus())
        || DataStatus.NEWBACK.equalsIgnoreCase(projectContent.getDataStatus()))
        && mUser.getJobNumber().equalsIgnoreCase(projectContent.getRegStaffId())) {
      return true;
    } else {
      ToastUtil.show(R.string.onlyCreater);
      return false;
    }
  }

  @Override public String getUrl() {
    return ApiConst.ULR_PROJECT_LEDGER_PROPERTY_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("memberCode", mUser.getMemberCode());
    params.put("projectId", projectContent.getId());
    params.put("storeId", projectContent.getStoreId());
    params.put("type", "mobile");
    return params.getParams();
  }

  @Override public void resolveResponse(String json) {
    StageDefinePropertyOriginal original =
        new Gson().fromJson(json, StageDefinePropertyOriginal.class);
    List<StageDefinePropertyOriginal.StageDefinePropertyContent> rows = original.getRows();
    if (BaseEntity.SUCCESS.equalsIgnoreCase(original.getResult())) {
      if (getCurrentPage() == 1) {
        mList.clear();
      }
      mList.addAll(rows);
      if (madapter == null) {
        madapter = new StageDefinePropertyListAdapter(getActivity(), mList);
        setListAdapter(madapter);
      } else {
        madapter.notifyDataSetChanged();
      }
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    StageDefinePropertyOriginal.StageDefinePropertyContent content = madapter.getItem(position - 1);

    Intent i = new Intent(getActivity(), ProjectLedgerPropertyDetailActivity.class);
    i.putExtra(ProjectLedgerPropertyDetailFragment.EXTRA_CONTENT, content);
    i.putExtra(ProjectLedgerPropertyDetailFragment.EXTRA_VALUE, projectContent);
    startActivityForResult(i, DETAIL);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == DETAIL) {
      refreshFromTop();//从顶部刷新
      volleyPost();
    }
  }

  public static ProjectLedgerPropertyListFrament newInstance(ProjectLedger content) {
    Bundle args = new Bundle();
    args.putSerializable(ProjectLedgerPagerActivity.EXTRA_CONTENT, content);
    ProjectLedgerPropertyListFrament fragment = new ProjectLedgerPropertyListFrament();
    fragment.setArguments(args);
    return fragment;
  }
}
